package rus.webapp.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;

import rus.model.ContentImage;
import rus.model.Product;
import rus.model.ProductCategory;
import rus.model.Shop;
import rus.service.ContentImageManager;
import rus.service.ProductCategoryManager;
import rus.service.ProductManager;
import rus.service.ShopManager;
import rus.webapp.util.Currency;

public class ProductForm extends BasePage implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2498288998567498644L;
	private Product product = new Product();
	private ProductManager productManager;	
	private ContentImageManager contentImageManager;
	private ShopManager shopManager;
	private ProductCategoryManager productCategoryManager;
	private UploadedFile file;
	private Long id;
	private Integer selectedShopId;
	private Integer selectedCategoryId;
	
	private String aliquot;
	private String fraction;
	
	private HtmlSelectOneMenu selectItem = new HtmlSelectOneMenu();
	private HtmlSelectOneMenu selectFractionItem = new HtmlSelectOneMenu();
	
	
	public String callCreate(){
		log.info("Call ctreating new product");
		product = new Product();
		product.setCurrency(Currency.UAH.toString());
		return SUCCESS;
	}
	
	public String create() throws IOException{
		ContentImage contentImage = null;
		byte[] byteArray = null;
		
		if(getFile() != null){
		contentImage =	new ContentImage();
		
		InputStream stream = file.getInputStream();
		ByteArrayOutputStream bAOut = new ByteArrayOutputStream();
		int c;
		while ((c = stream.read()) != -1) {
			bAOut.write((char) c);
		}
			byteArray = bAOut.toByteArray();
			contentImage.setURI(StringUtils.replace(StringUtils.lowerCase("product/" + product.getName()), " ", "/"));
			contentImage.setData(byteArray);
			getContentImageManager().saveContentImage(contentImage);
		}		
		
		product.setImage(contentImage);
		product.setURL(validateURL(product.getURL()));
		product.setShop(shopManager.getShopById(selectedShopId));
		product.setProductCategory(productCategoryManager.getProductCategoryById(selectedCategoryId));		
		product.setPrice(Long.parseLong(aliquot + validateFraction(fraction)));
		productManager.createProduct(product);
		
		
		return SUCCESS;
	}
	
	public List<SelectItem> getShopList(){
		List<SelectItem> shopNamesList = new ArrayList<SelectItem>();		
		for (Shop shopItem : shopManager.getAll()) {
			shopNamesList.add(new SelectItem(shopItem.getId(), shopItem.getName()));
			
		}		
		return shopNamesList;
	}
	
	public List<SelectItem> getCategoryList(){
		List<SelectItem> categoriesList = new ArrayList<SelectItem>();
		for (ProductCategory item : productCategoryManager.getList()) {
			categoriesList.add(new SelectItem(item.getId(), item.getName()));
		}
		return categoriesList;
	}
	
	public List<Product> getProductList(){
		return productManager.getProductList();
	}
	
	public String callEditProduct(){
		setProduct(productManager.getById(getId()));
		int aliquot = (int) (product.getPrice()/100);
		int fraction = (int) (product.getPrice() - aliquot*100);
		setAliquot(Integer.toString(aliquot));
		setFraction(validateFraction(Integer.toString(fraction)));
		selectItem.setValue(product.getShop().getId());
		selectFractionItem.setValue(product.getProductCategory().getId());
		return EDIT;
	}
	
	public String delete(){
		productManager.delete(product);
		return DELETE;
	}
	
	public String validateFraction(String fraction){
		if(fraction.length()!=2){
			if(fraction.length()<2){
				fraction+="0";
			}else{
				fraction = StringUtils.substring(fraction, 0, 2);
			}
		}
		return fraction;
	}
	
	
	public String execute(){
			log.debug("Test log....");
		return "success";
	}


	public void setProduct(Product product) {
		this.product = product;
	}


	public Product getProduct() {
		return product;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	public ProductManager getProductManager() {
		return productManager;
	}

	public void setContentImageManager(ContentImageManager contentImageManager) {
		this.contentImageManager = contentImageManager;
	}

	public ContentImageManager getContentImageManager() {
		return contentImageManager;
	}

	public void setShopManager(ShopManager shopManager) {
		this.shopManager = shopManager;
	}

	public ShopManager getShopManager() {
		return shopManager;
	}

	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public ProductCategoryManager getProductCategoryManager() {
		return productCategoryManager;
	}

	public void setSelectedCategoryId(Integer selectedCategoryId) {
		this.selectedCategoryId = selectedCategoryId;
	}

	public Integer getSelectedCategoryId() {
		return selectedCategoryId;
	}

	public void setSelectedShopId(Integer selectedShopId) {
		this.selectedShopId = selectedShopId;
	}

	public Integer getSelectedShopId() {
		return selectedShopId;
	}

	public void setAliquot(String aliquot) {
		this.aliquot = aliquot;
	}

	public String getAliquot() {
		return aliquot;
	}

	public void setFraction(String fraction) {
		this.fraction = fraction;
	}

	public String getFraction() {
		return fraction;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setSelectItem(HtmlSelectOneMenu selectItem) {
		this.selectItem = selectItem;
	}

	public HtmlSelectOneMenu getSelectItem() {
		return selectItem;
	}

	public void setSelectFractionItem(HtmlSelectOneMenu selectFractionItem) {
		this.selectFractionItem = selectFractionItem;
	}

	public HtmlSelectOneMenu getSelectFractionItem() {
		return selectFractionItem;
	}


}
